#include <iostream>
#include "Core/Blue.h"

using namespace std;
using namespace blue;


int main(int argc, char **argv)
{
  int width = 1024, height = 768;
  bool fullscreen = false;

  if(argc >= 3)
    {
      istringstream iss1(argv[1]);
      iss1 >> width;
      istringstream iss2(argv[2]);
      iss2 >> height;
      if(argc >= 4)
	fullscreen = true;
    }

  Device *device = Device::instance();
  Renderer *rdr = device->getRenderer();
  EventManager *emgr = device->getEventManager();

  device->init(width, height, fullscreen);

  rdr->initExtensions();

  ///Scene
  Mesh *scene = new Mesh("media/scene.obj");
  scene->setTexture("textures/rock.tga");
  scene->setScale(Vector3d(3, 3, 3));

  Mesh *scene2 = new Mesh("media/monkey.obj");
  scene2->setTexture("textures/suzanne.bmp");
  scene2->setPosition(Vector3d(0, 0, 4));

  FreeFlyCamera *cam = new FreeFlyCamera(Vector3d(1, 5, 5));
  cam->setSensivity(0.2);
  cam->setSpeed(5.0);

  Shader *light = new Shader("shaders/light.vert", "shaders/light.frag");
  Shader *cartoon = new Shader("shaders/celshading.vert", "shaders/celshading.frag");
  Shader *shader = NULL;

  Image *sceneT = new Image("textures/suzanne.bmp", 0, device->getSize().Y-100);
  sceneT->setSize(100, 100);
  Image *scene2T = new Image("textures/rock.tga", sceneT->getSize().X, device->getSize().Y-100);
  scene2T->setSize(100, 100);

  Skybox *skybox = new Skybox("textures/back.jpg", "textures/front.jpg",
			      "textures/bottom.jpg", "textures/top.jpg",
			      "textures/right.jpg", "textures/left.jpg");
  skybox->setSize(10.0f);

  device->showCursor(false);
 
  while(device->run())
    {
      device->setWindowTitle("Blue3D : test des VBOs, OpenGL", true);
      emgr->getEvent();
      cam->onEvent();
      cam->animate();
      skybox->setPosition(cam->getPosition());

      rdr->beginScene();
      cam->look();
      skybox->draw();
      scene->draw();
      scene2->draw();
 
      if(shader) shader->setShader(false);

      rdr->set2DMode();
      sceneT->draw();
      scene2T->draw();
      rdr->end2DMode();

      if(shader) shader->setShader(true);

      rdr->endScene();

      if(emgr->quit()) device->closeDevice();
      else if(emgr->isKeyDownOnce(GLFW_KEY_ESC)) device->closeDevice();
      else if(emgr->isKeyDownOnce('A')) device->showCursor(false);
      else if(emgr->isKeyDownOnce('E')) device->showCursor(true);
      else if(emgr->isKeyDownOnce('I'))
        {
	  cartoon->setShader(false);
	  light->setShader(false);
	  shader = NULL;
        }
      else if(emgr->isKeyDownOnce('O'))
        {
	  cartoon->setShader(false);
	  light->setShader(true);
	  shader = light;
        }
      else if(emgr->isKeyDownOnce('P'))
        {
	  light->setShader(false);
	  cartoon->setShader(true);
	  shader = cartoon;
        }
      else if(emgr->isKeyDownOnce('K'))
	rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
      else if(emgr->isKeyDownOnce('L'))
	rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      else if(emgr->isKeyDownOnce('M'))
	rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

  delete cam;
  delete skybox;
  delete sceneT;
  delete scene2T;
  delete scene;
  delete scene2;
  delete cartoon;
  delete light;

  Device::destroy();

  return 0;
}

